package org.dromara.pay.crud.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.service.pay.PayActivityRegistrationTranslationService;
    import org.dromara.common.mybatis.core.page.TableDataInfo;
    import org.dromara.common.mybatis.core.page.PageQuery;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.cache.annotation.Cacheable;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.pay.domain.bo.PayActivityRegistrationBo;
import org.dromara.pay.domain.vo.PayActivityRegistrationVo;
import org.dromara.pay.domain.PayActivityRegistration;
import org.dromara.pay.mapper.PayActivityRegistrationMapper;
import org.dromara.pay.crud.service.IPayActivityRegistrationCrudService;
import org.dromara.pay.domain.query.PayActivityRegistrationQuery;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 活动报名Service业务层处理
 *
 * @author yf
 * @date 2024-07-22
 */
@RequiredArgsConstructor
@Service
public class PayActivityRegistrationCrudServiceImpl implements IPayActivityRegistrationCrudService, PayActivityRegistrationTranslationService {

    private final PayActivityRegistrationMapper baseMapper;

    /**
     * 查询活动报名
     *
     * @param registrationId 主键
     * @return 活动报名
     */
    @Override
    public PayActivityRegistrationVo queryById(Integer registrationId){
        return baseMapper.selectVoById(registrationId);
    }


    /**
     * 分页查询活动报名列表
     *
     * @param query        查询条件
     * @param pageQuery 分页参数
     * @return 活动报名分页列表
     */
     @Override
     public TableDataInfo<PayActivityRegistrationVo> queryPageList(PayActivityRegistrationQuery query, PageQuery pageQuery) {
        LambdaQueryWrapper<PayActivityRegistration> lqw = buildQueryWrapper(query);
        Page<PayActivityRegistrationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
     }


    /**
     * 查询符合条件的活动报名列表
     *
     * @param query 查询条件
     * @return 活动报名列表
     */
    @Override
    public List<PayActivityRegistrationVo> queryList(PayActivityRegistrationQuery query) {
        LambdaQueryWrapper<PayActivityRegistration> lqw = buildQueryWrapper(query);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PayActivityRegistration> buildQueryWrapper(PayActivityRegistrationQuery query) {
        Map<String, Object> params = query.getParams();
        LambdaQueryWrapper<PayActivityRegistration> lqw = Wrappers.lambdaQuery();
                    lqw.eq(StringUtils.isNotBlank(query.getRegistrationStatus()), PayActivityRegistration::getRegistrationStatus, query.getRegistrationStatus());
                    lqw.eq(StringUtils.isNotBlank(query.getRegistrationWay()), PayActivityRegistration::getRegistrationWay, query.getRegistrationWay());
                    lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
                        PayActivityRegistration::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        return lqw;
    }



    /**
     * 新增活动报名
     *
     * @param bo 活动报名
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(PayActivityRegistrationBo bo) {
        PayActivityRegistration add = MapstructUtils.convert(bo, PayActivityRegistration.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRegistrationId(add.getRegistrationId());
        }
        return flag;
    }

    /**
     * 修改活动报名
     *
     * @param bo 活动报名
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(PayActivityRegistrationBo bo) {
        PayActivityRegistration update = MapstructUtils.convert(bo, PayActivityRegistration.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PayActivityRegistration entity){
        //SAIL 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除活动报名信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //SAIL 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 翻译活动报名信息
     *
     * @param id 主键
     * @return ${vo}
     */
    @Override
    @Cacheable(cacheNames = PayActivityRegistration.TRANS_ID_TO_OBJECT, key = "#id")
    public Object selectObjectById(Integer id){
        return baseMapper.selectVoById(id);
    }
}
